﻿/*
 * @author: wizardc
 */

using System;
using UnityEngine;
using UnityEngine.UI;

namespace Dou.UI
{
    /// <summary>
    /// 图集图片
    /// </summary>
    [ExecuteInEditMode]
    public class DImage : Image
    {
        private string _spriteName = string.Empty;
        [SerializeField]
        private bool _useGray = false;
        [SerializeField]
        private bool _useNativeSize = false;

        private PolygonCollider2D _collider2D;

        /// <summary>
        /// 精灵资源名称
        /// </summary>
        public string spriteName
        {
            set => SetSpriteName(value);
            get => _spriteName;
        }

        /// <summary>
        /// 是否置灰
        /// </summary>
        public bool useGray
        {
            set
            {
                if (_useGray != value)
                {
                    _useGray = value;
                    SetGray(_useGray);
                }
            }
            get => _useGray;
        }

        /// <summary>
        /// 是否默认使用原始尺寸
        /// </summary>
        public bool useNativeSize
        {
            set
            {
                if (_useNativeSize != value)
                {
                    _useNativeSize = value;
                    if (_useNativeSize)
                    {
                        SetNativeSize();
                    }
                }
            }
            get => _useNativeSize;
        }

        protected override void Awake()
        {
            base.Awake();
            if (_useGray)
            {
                SetGray(true);
            }
            _collider2D = GetComponent<PolygonCollider2D>();
        }

        public void SetSpriteName(string spriteName, Action callback = null)
        {
            if (_spriteName.Equals(spriteName))
            {
                return;
            }
            if (!string.IsNullOrEmpty(_spriteName))
            {
                sprite = UIAssetUtil.transparentSprite;
                if (!UIAssetUtil.ReleaseObject(_spriteName))
                {
                    Debug.LogWarning($"资源{_spriteName}回收失败！");
                }
            }
            _spriteName = spriteName;
            if (!string.IsNullOrEmpty(_spriteName))
            {
                UIAssetUtil.LoadObject(_spriteName, (p, obj) =>
                {
                    // 回调之前又修改了精灵名称
                    if (p != _spriteName)
                    {
                        return;
                    }
                    if (gameObject != null && obj != null)
                    {
                        sprite = obj as Sprite;
                        if (_useNativeSize)
                        {
                            SetNativeSize();
                        }
                        callback?.Invoke();
                    }
                }, true, typeof(Sprite)); // Sprite 加载需要指定类型
            }
        }

        public void SetColor(float r, float g, float b, float a = 1)
        {
            color = new Color(r, g, b, a);
        }

        public void SetColor32(int r, int g, int b, int a = 255)
        {
            color = new Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
        }

        private void SetGray(bool isGray)
        {
            material = isGray ? UIAssetUtil.grayMat : null;
        }

        public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
        {
            if (_collider2D != null)
            {
                return _collider2D.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint));
            }
            return base.IsRaycastLocationValid(screenPoint, eventCamera);
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            if (!string.IsNullOrEmpty(_spriteName))
            {
                UIAssetUtil.ReleaseObject(_spriteName);
                _spriteName = null;
            }
        }
    }
}
